package org.zhang.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.zhang.shortlink.project.dao.entity.LinkOsStatsDO;
import org.zhang.shortlink.project.dto.req.ShortLinkStatsReqDTO;

import java.util.HashMap;
import java.util.List;

/**
* @author zhang
* @description 针对表【t_link_os_stats(短链接监控操作系统访问状态)】的数据库操作Mapper
* @createDate 2024-04-25 16:50:38
* @Entity org.zhang.shortlink.project.dao.entity.LinkOsStatsDO
*/
@Mapper
public interface LinkOsStatsMapper extends BaseMapper<LinkOsStatsDO> {

    @Insert("""
            INSERT INTO t_link_os_stats (id,full_short_url,date,cnt,os,create_time,update_time,del_flag)
            VALUES (#{id},#{fullShortUrl}, #{date}, #{cnt},#{os}, #{createTime},#{updateTime},#{delFlag})
            ON DUPLICATE KEY UPDATE cnt = cnt + #{cnt};
            """)
    void shortLinkOsStats(LinkOsStatsDO linkOsStatsDO);

    /**
     * 根据短链接获取指定日期内操作系统监控数据
     */
    @Select("SELECT " +
            "    os, " +
            "    SUM(cnt) AS count " +
            "FROM " +
            "    t_link_os_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, os;")
    List<HashMap<String, Object>> listOsStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

}




